package com.yc.login.web;

//import com.yc.login.bean.Result;
//import com.yc.login.biz.BizException;
//import com.yc.login.biz.UserBiz;
import com.yc.login.bean.Result;
import com.yc.login.biz.BizException;
import com.yc.login.biz.UserBiz;
import com.yc.login.dao.UserDao;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserAction {
    @Resource
    UserDao userDao;

    @Resource
    UserBiz userBiz;

    @GetMapping("showParams")
    public String showParams(){
        return userDao.toString();
    }

    @RequestMapping("login")
    public Result login(String username, String password, HttpSession session){
        try {
            Map<String,Object> user = userBiz.login(username,password);
            session.setAttribute("loginedUser",user);
            return new Result(1,"登录成功!",null);
        } catch (BizException e) {
            e.printStackTrace();
            return new Result(0,e.getMessage(),null);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(0,"业务繁忙,请稍后再试!",null);
        }
    }

    @RequestMapping("logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return new Result(1,"退出成功!",null);
    }

    @RequestMapping("getLoginedUser")
    public Result getLoginedUser(HttpSession session){
        Object loginedUser = session.getAttribute("loginedUser");
        if (loginedUser == null){
            return new Result(0,"用户未登录!",null);
        }else {
            return new Result(1,"用户已登录!",loginedUser);
        }
    }
}
